package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.view.View;

import com.hjq.widget.layout.SettingBar;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.dialog.BottomDelayTimeDialog;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;
import com.qiantu.phone.utils.TempDataHolder;
import com.lxj.xpopup.XPopup;

import java.io.Serializable;
import java.util.Map;

public class AddTaskActivity extends AppActivity {
    public static final int TYPE_SCENE = 1;
    public static final int TYPE_LINKAGE = 2;

//    private SettingBar deviceTask;
//    private SettingBar delayTask;
//    private SettingBar pushTask;
//    private SettingBar linkageTask;
    private SettingBar oneKeyTask;
    private BottomDelayTimeDialog mBottomDelayTimeDialog;
    private int mPosition;
    private int type;
    private int sceneType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_task;
    }

    @Override
    protected void initView() {
//        deviceTask = findViewById(R.id.device_task);
//        delayTask = findViewById(R.id.delay_task);
//        pushTask = findViewById(R.id.push_task);
//        linkageTask = findViewById(R.id.linkage_task);
        oneKeyTask = findViewById(R.id.one_key_scene_task);
        type = getInt("type");
        sceneType = getInt("sceneType", 1);
        switch (type) {
            case TYPE_SCENE:
                oneKeyTask.setVisibility(View.GONE);
                break;
            case TYPE_LINKAGE:
                oneKeyTask.setVisibility(View.VISIBLE);
                break;
        }
        setOnClickListener(R.id.device_task, R.id.delay_task, R.id.push_task, R.id.linkage_task, R.id.one_key_scene_task);
    }

    @Override
    protected void initData() {
        mPosition = getIntent().getIntExtra("position", -1);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.device_task: {
                Map<String,Integer> taskData = (Map<String, Integer>) getIntent().getSerializableExtra("taskData");
                Intent intent = new Intent(this, DeviceActionActivity.class);
                intent.putExtra("position", mPosition);
                intent.putExtra("type", type);
                intent.putExtra("sceneType", sceneType);
                intent.putExtra("taskData", (Serializable) taskData);
                startActivity(intent);
                break;
            }
            case R.id.delay_task: {
                showBottomTimeDialog();
                break;
            }
            case R.id.push_task: {
                Intent intent = new Intent(this, PushMessageSettingActivity.class);
                intent.putExtra("position", mPosition);
                intent.putExtra("type", type);
                intent.putExtra("sceneType", sceneType);
                startActivity(intent);
                break;
            }
            case R.id.linkage_task: {
                Intent intent = new Intent(this, LinkageStartStopActivity.class);
                intent.putExtra("position", mPosition);
                intent.putExtra("type", type);
                intent.putExtra("sceneType", sceneType);
                startActivity(intent);
                break;
            }
            case R.id.one_key_scene_task: {
                Intent intent = new Intent(this, OnKeySceneSelectActivity.class);
                intent.putExtra("position", mPosition);
                startActivity(intent);
                break;
            }
        }
    }

    private void showBottomTimeDialog() {
        if (mBottomDelayTimeDialog == null) {
            mBottomDelayTimeDialog = new BottomDelayTimeDialog(this);
            mBottomDelayTimeDialog.setDialogClickListener(new BottomDelayTimeDialog.DialogClickListener() {
                @Override
                public void onConfirmClick(String time, int longTime) {
                    mBottomDelayTimeDialog.dismiss();
                    if(type == TYPE_LINKAGE){
                        LLSceneLinkageUtil.addDelayTask(LinkageDataHolder.getLinkageData(), longTime, mPosition);
                        startActivity_SingleTask(EditLinkageActivity.class);
                    }else if(type == TYPE_SCENE){
                        LLSceneLinkageUtil.addDelayTask(TempDataHolder.getEditSceneDetailBean(sceneType), longTime, mPosition);
                        switch (sceneType){
                            case 1:
                                startActivity_SingleTask(EditSceneActivityNew.class);
                                break;
                            case 3:
                                startActivity_SingleTask(EditSceneIntentActivity.class);
                                break;
                            case 4:
                                startActivity_SingleTask(EditScenePrivateActivity.class);
                                break;
                        }
                    }
                }
            });
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomDelayTimeDialog);
        }
        mBottomDelayTimeDialog.show();
    }
}